﻿using System;
using System.Drawing;
using System.Windows.Forms;

namespace AyuBookmark.WinForm.Controls
{
    /// <summary>
    /// 带进度条和状态图标的状态栏控件
    /// </summary>
    public class ProgressStatusBar : StatusStrip
    {
        private ToolStripStatusLabel lblStatus;
        private ToolStripProgressBar progressBar;
        private ToolStripStatusLabel lblIcon;
        private System.Windows.Forms.Timer progressTimer;

        // 默认图标定义
        private static readonly Icon DefaultReadyIcon = SystemIcons.Information;
        private static readonly Icon DefaultWorkingIcon = SystemIcons.Exclamation;
        private static readonly Icon DefaultSuccessIcon = SystemIcons.Shield;
        private static readonly Icon DefaultErrorIcon = SystemIcons.Error;

        public ProgressStatusBar()
        {
            InitializeComponents();
        }

        private void InitializeComponents()
        {
            // 基础设置
            this.SuspendLayout();
            this.Dock = DockStyle.Bottom;
            this.BackColor = SystemColors.Control;
            this.Font = new Font("Microsoft YaHei", 9f);

            // 状态图标
            this.lblIcon = new ToolStripStatusLabel();
            this.lblIcon.Image = DefaultReadyIcon.ToBitmap();
            this.lblIcon.ImageScaling = ToolStripItemImageScaling.None;
            this.lblIcon.Padding = new Padding(5, 0, 5, 0);

            // 状态文本
            this.lblStatus = new ToolStripStatusLabel();
            this.lblStatus.Text = "就绪";
            this.lblStatus.Spring = true;
            this.lblStatus.TextAlign = ContentAlignment.MiddleLeft;

            // 进度条
            this.progressBar = new ToolStripProgressBar();
            this.progressBar.Visible = false;
            this.progressBar.Width = 150;
            this.progressBar.Style = ProgressBarStyle.Continuous;

            // 添加控件
            this.Items.AddRange(new ToolStripItem[] {
                this.lblIcon,
                this.lblStatus,
                this.progressBar
            });

            // 进度计时器（用于模拟进度）
            this.progressTimer = new System.Windows.Forms.Timer();
            this.progressTimer.Interval = 100;
            this.progressTimer.Tick += (s, e) => {
                if (progressBar.Value < progressBar.Maximum)
                    progressBar.Value++;
                else
                    StopProgress();
            };

            this.ResumeLayout(false);
        }

        #region 公共方法
        /// <summary>
        /// 设置状态文本
        /// </summary>
        public void SetStatus(string text)
        {
            this.lblStatus.Text = text;
        }

        /// <summary>
        /// 设置状态图标
        /// </summary>
        public void SetStatusIcon(Image icon)
        {
            this.lblIcon.Image = icon;
        }

        /// <summary>
        /// 开始显示进度条
        /// </summary>
        public void StartProgress(int maxValue = 100)
        {
            this.progressBar.Maximum = maxValue;
            this.progressBar.Value = 0;
            this.progressBar.Visible = true;
            this.progressTimer.Start();
        }

        /// <summary>
        /// 停止进度条并隐藏
        /// </summary>
        public void StopProgress()
        {
            this.progressTimer.Stop();
            this.progressBar.Visible = false;
        }

        /// <summary>
        /// 更新进度值
        /// </summary>
        public void UpdateProgress(int value)
        {
            if (value <= progressBar.Maximum)
                progressBar.Value = value;
        }
        #endregion

        #region 预定义状态
        public void ShowReady()
        {
            SetStatus("就绪");
            SetStatusIcon(DefaultReadyIcon.ToBitmap());
            StopProgress();
        }

        public void ShowWorking(string message)
        {
            SetStatus(message);
            SetStatusIcon(DefaultWorkingIcon.ToBitmap());
            StartProgress();
        }

        public void ShowSuccess(string message)
        {
            SetStatus(message);
            SetStatusIcon(DefaultSuccessIcon.ToBitmap());
            StopProgress();
        }

        public void ShowError(string message)
        {
            SetStatus(message);
            SetStatusIcon(DefaultErrorIcon.ToBitmap());
            StopProgress();
        }
        #endregion

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                progressTimer?.Dispose();
            }
            base.Dispose(disposing);
        }
    }
}